package com.example.yls.demoa;

import android.animation.AnimatorInflater;
import android.animation.ValueAnimator;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.TransitionDrawable;
import android.renderscript.Sampler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.transition.Transition;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private ImageView iv_wifi;
    private Button btn1;
    private Button btn2;
    private AnimationDrawable animationDrawable;
    private ImageView iv_piture;
    private Button btn_value;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv_wifi=findViewById(R.id.iv_wifi);
        btn1=findViewById(R.id.btn_start);
        btn2=findViewById(R.id.btn_stop);
        iv_piture=findViewById(R.id.iv_piture);
        btn_value=findViewById(R.id.btn_value);
        animationDrawable= (AnimationDrawable) iv_wifi.getDrawable();
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //animationDrawable.start();
                //实现图片透明化
               // staranimAlpha();
                //实现图片平移
               //starTranslate();
                //实现图片缩放：scale
                //startScale();
                //实现图片旋转
                //starRotate();
                //动画集合
               // startanimalset();
            }


        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                animationDrawable.stop();

            }
        });
        //属性动画
        btn_value.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               /* ValueAnimator valueAnimator=ValueAnimator.ofInt(
                        btn_value.getLayoutParams().width,500);
                valueAnimator.setDuration(3000);
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                      int value= (int) valueAnimator.getAnimatedValue();
                        Log.e(TAG,"value="+value);
                        btn_value.getLayoutParams().width=value;
                        btn_value.requestLayout();
                    }
                });
                valueAnimator.setRepeatCount(3);
                valueAnimator.setRepeatMode(ValueAnimator.REVERSE);
               valueAnimator.start();*/

               //第二种方法，利用xml
                ValueAnimator valueAnimator= (ValueAnimator) AnimatorInflater.loadAnimator(MainActivity.this,
                        R.animator.set_ani);
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        int value= (int) valueAnimator.getAnimatedValue();
                        Log.e(TAG,"value="+value);
                        btn_value.getLayoutParams().width=value;
                        btn_value.getLayoutParams().height=value;
                        btn_value.setText("属性动画"+value);
                        btn_value.setBackgroundColor(Color.argb(255,value/2,0,0));
                        btn_value.requestLayout();
                    }
                });

                valueAnimator.start();
            }
        });
    }

    private void startanimalset() {
       /* AnimationSet animationSet=new AnimationSet(true);
        TranslateAnimation translateAnimation=new TranslateAnimation(0,400,0,360);
        RotateAnimation rotateAnimation=new RotateAnimation(0,360,180,180);
        ScaleAnimation scaleAnimation=new ScaleAnimation(1,3,1,3,180,180);
        animationSet.addAnimation(translateAnimation);
        animationSet.addAnimation(rotateAnimation);
        animationSet.addAnimation(scaleAnimation);
        animationSet.setDuration(3000);
        animationSet.setRepeatCount(3);
        animationSet.setRepeatMode(Animation.REVERSE);
        iv_piture.startAnimation(animationSet);
*/
       //xml
        AnimationSet animationSet= (AnimationSet) AnimationUtils.loadAnimation(MainActivity.this,
                R.anim.animalset);
        iv_piture.startAnimation(animationSet);
    }

    private void starRotate() {
        /*RotateAnimation rotateAnimation=new RotateAnimation(0,360,180,180);
        rotateAnimation.setDuration(3000);
        rotateAnimation.setFillAfter(true);
        iv_piture.startAnimation(rotateAnimation);*/
        //xml
        RotateAnimation rotateAnimation= (RotateAnimation) AnimationUtils.loadAnimation(MainActivity.this,
                R.anim.rotate);
        iv_piture.startAnimation(rotateAnimation);
    }

    private void startScale() {
        /*ScaleAnimation scaleAnimation=new ScaleAnimation(1,3,1,3);
        scaleAnimation.setDuration(3000);
        scaleAnimation.setFillAfter(true);
        iv_piture.startAnimation(scaleAnimation);*/
        /*xml代码*/
        ScaleAnimation scaleAnimation= (ScaleAnimation) AnimationUtils.loadAnimation(MainActivity.this
        ,R.anim.scale);
        iv_piture.startAnimation(scaleAnimation);


    }

    private void starTranslate() {
        
        /*TranslateAnimation translateAnimation=new TranslateAnimation(0,500,0,500);
        // 1. fromXDelta ：视图在水平方向x 移动的起始值
        // 2. toXDelta ：视图在水平方向x 移动的结束值
        // 3. fromYDelta ：视图在竖直方向y 移动的起始值
        // 4. toYDelta：视图在竖直方向y 移动的结束值
        translateAnimation.setDuration(3000);
        translateAnimation.setFillAfter(true);
        translateAnimation.setRepeatCount(-1);
        translateAnimation.setRepeatMode(Animation.REVERSE);
        iv_piture.startAnimation(translateAnimation);*/
        /*使用xml*/
        TranslateAnimation translateAnimation= (TranslateAnimation) AnimationUtils.loadAnimation(
                MainActivity.this,R.anim.translate);
        iv_piture.startAnimation(translateAnimation);
    }

    private void staranimAlpha() {
     /* AlphaAnimation alphaAnimation=
              (AlphaAnimation) AnimationUtils.loadAnimation(MainActivity.this,
                R.anim.alpha);*/
     AlphaAnimation alphaAnimation=new AlphaAnimation(1,0);
     alphaAnimation.setDuration(3000);
     alphaAnimation.setFillAfter(true);
     alphaAnimation.setRepeatCount(3);
     alphaAnimation.setRepeatMode(Animation.REVERSE);
     iv_piture.startAnimation(alphaAnimation);

    }
}
